package com.dondedesigns.eh.controller.profile;

import java.util.Arrays;
import java.util.List;

import com.dondedesigns.eh.controller.index.UserAware;
import com.dondedesigns.eh.persistence.AliveStatus;
import com.dondedesigns.eh.persistence.BlUser;
import com.dondedesigns.eh.persistence.Rank;
import com.dondedesigns.eh.persistence.UgStatus;
import com.dondedesigns.eh.persistence.User;
import com.dondedesigns.eh.persistence.dao.BlUserDAO;
import com.dondedesigns.eh.persistence.dao.UserDAO;
import com.dondedesigns.eh.util.MessageConstants;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class BLName extends ActionSupport implements UserAware, ModelDriven<User> {
	
	private User user;
	private BlUser blUser = new BlUser();
	private UserDAO userDAO;
	private BlUserDAO blUserDAO;
	
	private String blRank;
	private String blAliveStatus;
	private String blUgStatus;
	
	@Override
	public String execute() throws Exception {
		blUser.setAliveStatus(getBlAliveStatus());
		blUser.setUgStatus(blUgStatus);
		blUser.setRank(blRank);
		user.addBlUser(blUser);
		userDAO.update(user);
		return SUCCESS;
	}
	
	public BlUser getBlUser() {
		return blUser;
	}

	public void setBlUser(BlUser blUser) {
		this.blUser = blUser;
	}

	@Override
	public void setUser(User user) {
		this.user = user;

	}

	public String getBlRank() {
		return blRank;
	}

	public void setBlRank(String blRank) {
		this.blRank = blRank;
	}

	public String getBlAliveStatus() {
		return blAliveStatus;
	}

	public void setBlAliveStatus(String blAliveStatus) {
		this.blAliveStatus = blAliveStatus;
	}

	public String getBlUgStatus() {
		return blUgStatus;
	}

	public void setBlUgStatus(String blUgStatus) {
		this.blUgStatus = blUgStatus;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	
	public void setBlUserDAO(BlUserDAO blUserDAO) {
		this.blUserDAO = blUserDAO;
	}

	@Override
	public User getModel() {
		return user;
	}
	
	public List<UgStatus> getListUgStatus() {
		return Arrays.asList(UgStatus.values());

	}
	
	public List<AliveStatus> getListAliveStatus() {
		return Arrays.asList(AliveStatus.values());
	}
	
	public List<Rank> getListRanks() {
		return Arrays.asList(Rank.values());

	}
	
	@Override
	public void validate() {
		if (blUser.getUsername() == null || blUser.getUsername().isEmpty()) {
			addActionError(getText(MessageConstants.BLUSER_REQUIRED));
		} else if (!blUser.getUsername().matches("[A-Za-z0-9_]*")) {
			addActionError(getText(MessageConstants.BLUSER_ERROR));
		} else if (blUserDAO.exists(blUser)) {
			addActionError(getText(MessageConstants.BLUSER_EXISTS));
		}
		
		if (blAliveStatus == null || blAliveStatus.isEmpty()) {
			addActionError(getText(MessageConstants.ALIVESTATUS_REQUIRED));
		}
		if (blRank == null || blRank.isEmpty()) {
			addActionError(getText(MessageConstants.RANK_REQUIRED));
		}
		if (blUgStatus == null || blUgStatus.isEmpty()) {
			addActionError(getText(MessageConstants.UGSTATUS_REQUIRED));
		}
	}

}
